package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;
    @Transactional
    @Override
    public void saveWithMealDish(SetmealDto setmealDto) {
        //向套餐表插入数据
        this.save(setmealDto);
        setmealDto.getSetmealDishes().stream().peek(item->{
           //设置套餐id
           item.setSetmealId(setmealDto.getId());
        }).collect(Collectors.toList());
        //向套餐菜品关系表插入数据
        setmealDishService.saveBatch(setmealDto.getSetmealDishes());
    }

    /**
     * 查询每页数据
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<SetmealDto> getPage(Long page, Long pageSize, String name) {
        //分页构造器设置页码每页数量
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        //复制分页构造器展示分类名称
        Page<SetmealDto> setmealDtoPage = new Page<>(page,pageSize);
        //查询当前页套餐信息,根据更新时间降序排序
        this.page(setmealPage,new LambdaQueryWrapper<Setmeal>().like(name!=null,Setmeal::getName,name)
                .orderByDesc(Setmeal::getUpdateTime));
        //复制当前页数据
        BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");
        //查询套餐名称，添加至dto
        List<SetmealDto> dtoList = setmealPage.getRecords().stream().map(item -> {
            SetmealDto setmealDto = new SetmealDto();
            //复制套餐数据
            BeanUtils.copyProperties(item, setmealDto);
            //根据套餐id查询套餐名称
            Category category = categoryService.getById(item.getCategoryId());
            //设置套餐名
            setmealDto.setCategoryName(category.getName());
            return setmealDto;
        }).collect(Collectors.toList());
        setmealDtoPage.setRecords(dtoList);
        return setmealDtoPage;
    }

    /**
     * 删除套餐
     * @param ids
     */
    @Transactional
    @Override
    public void removeWithDish(List<Long> ids) {
        //查询套餐状态，确定是否可以删除
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.in(Setmeal::getId,ids);
        qw.eq(Setmeal::getStatus,1);

        int count = this.count(qw);
        if (count > 0) {
            //包含正在售卖状态，不能删除
            throw new CustomException("套餐正在售卖中，不能删除");
        }
        //如果可以删除，先删除setmeal
        this.removeByIds(ids);
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.in(SetmealDish::getSetmealId,ids);
        //删除关系表数据--setmeal_dish
        setmealDishService.remove(lqw);
    }

    /**
     * 回显套餐数据
     * @param id
     * @return
     */
    @Override
    public SetmealDto getSetmealDto(Long id) {
        //根据id查询套餐信息
        Setmeal setmeal = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        //设置口味数据
        List<SetmealDish> mealdishes = setmealDishService.list(new LambdaQueryWrapper<SetmealDish>().eq(SetmealDish::getSetmealId, id));
        setmealDto.setSetmealDishes(mealdishes);
        //设置套餐名称
        Category category = categoryService.getOne(new LambdaQueryWrapper<Category>().eq(Category::getId, setmeal.getCategoryId()));
        setmealDto.setCategoryName(category.getName());
        return setmealDto;
    }
    @Transactional
    @Override
    public void updateWithDish(SetmealDto setmealDto) {
        //更新套餐表
        this.updateById(setmealDto);
        //菜品插入dishid
        setmealDto.getSetmealDishes().stream()
                .peek(item-> item.setSetmealId(setmealDto.getId()))
                .collect(Collectors.toList());
        //清空id对应套餐菜品
        setmealDishService.remove(new LambdaQueryWrapper<SetmealDish>().eq(SetmealDish::getSetmealId,setmealDto.getId()));
        //插入新菜品
        setmealDishService.saveBatch(setmealDto.getSetmealDishes());
    }

    @Override
    public List<SetmealDish> listDishes(Long setmealId) {
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,setmealId);
        wrapper.orderByDesc(SetmealDish::getUpdateTime);
        List<SetmealDish> list = setmealDishService.list(wrapper);
        return list;
    }


}
